/*
 * @Descripttion: 配置检查。用于检查项目是否有该项配置，未配置则提示配置
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-26 09:31:08
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-26 20:40:51
 */
const deconfig =require('../common/deconfig');


module.exports =(options, app) => {
    return async function configHandler(ctx, next) {        
        var err ='请先在配置文件进行基础配置';        
        var data ={};
        var s ='';
        var str1 =`${new Date().toLocaleString()} [${ctx.ip}] [${ctx.method} ${ctx.url}] : ------ 【请先在配置文件进行基础配置】 ------ \n`;
        var res =false;   

        if(ctx.app.utils.validator.isNull(options)){               
            data ={
                msg:err,
                config_name_s:`${deconfig.configHandler.desc}`,
                config_name_e:`configHandler`,
                example:deconfig.configHandler.conf
            };     

            s ='';
            s +=ctx.app.config.console.request.style;
            s += str1;
            s +=`配置名称：${deconfig.configHandler.desc}\n`;   
            s +=`示例信息：${JSON.stringify(deconfig.configHandler.conf)}\n`;
            s +=ctx.app.config.console.request.style;
            console.log(s.bgRed);
            ctx.requestLogger.error(ctx.app.enums.response_code_enum.server_error.msg);
            return ctx.outError(null,data,ctx.app.enums.response_code_enum.server_error);  
        }else{
            res =true;
        }
        
        for(var key in deconfig){   
            if(!ctx.app.utils.isFunction(deconfig[key])){      
                if(!ctx.app.config.hasOwnProperty(key)){
                    console.log(key);
                    res =false;
                    data ={
                        msg:err,
                        config_name_s:`${deconfig[key].desc}`,
                        config_name_e:key,
                        example:deconfig[key].conf
                    };     
        
                    s ='';
                    s +=ctx.app.config.console.request.style;
                    s += str1;
                    s +=`配置名称：${deconfig[key].desc}\n`;   
                    s +=`示例信息：${JSON.stringify(deconfig[key].conf)}\n`;
                    s +=ctx.app.config.console.request.style;
                    console.log(s.bgRed);
                    ctx.requestLogger.error(ctx.app.enums.response_code_enum.server_error.msg);
                    return ctx.outError(null,data,ctx.app.enums.response_code_enum.server_error); 
                }else{
                    res =true;
                    continue;
                }  
            }else{                
                res =true;
                continue;
            }            
        }   
        
        if(res ===true){
            await next(options);
        }    
        
    };
};